from rest_framework_simplejwt.serializers import TokenObtainPairSerializer


class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
    '在荷载中添加自定义内容'

    @classmethod
    def get_token(cls, user):
        """
        重写get_token方法，它返回的token中就是荷载的内容
        自定义返回格式
        """
        token = super().get_token(user)  # 签发用户
        token['name'] = user.username  # 往荷载里面添加用户名称
        return token

    def validate(self, attrs):
        old_data = super().validate(attrs)
        data = {'code': 200,
                'msg': '登录成功',
                'username': self.user.username,
                'refresh': old_data['refresh'],
                'access': old_data['access']
                }
        return data
